#include "Matrix3.h"

#include <Rz/Memory.h>

namespace Rz { namespace Graphics { namespace Math {

Matrix3::Matrix3() :
	M11(1.0f), M21(0.0f), M31(0.0f),
	M12(0.0f), M22(1.0f), M32(0.0f),
	M13(0.0f), M23(0.0f), M33(1.0f)
{

}

Matrix3::Matrix3(const Matrix3& m)
{
	Memory::Copy(Cell, m.Cell, sizeof(Cell));
}

Matrix3::Matrix3(f32 m11, f32 m12, f32 m13,
		f32 m21, f32 m22, f32 m23,
		f32 m31, f32 m32, f32 m33) :
		M11(m11), M12(m12), M13(m13),
		M21(m21), M22(m22), M23(m23),
		M31(m31), M32(m32), M33(m33)
{

}

Matrix3& Matrix3::operator = (const Matrix3& m)
{
	Memory::Copy(Cell, m.Cell, sizeof(Cell));
	return *this;
}

f32 Matrix3::GetCell(u32 index) 
{
	// if (x >= 0 && x <= 2)
	return Cell[index];
}

f32 Matrix3::GetCell(u32 x, u32 y)
{
	// if (x >= 0 && y >= 0 && x <= 2 && y <= 2)
	return Cell[y * 3 + x];
}

void Matrix3::SetCell(u32 index, f32 value)
{
	// if (x >= 0 && x <= 2)
	// return *(Cell + index) = value;
	Cell[index] = value;
}

void Matrix3::SetCell(u32 x, u32 y, f32 value)
{
	// if (x >= 0 && y >= 0 && x <= 2 && y <= 2)
	Cell[y * 3 + x] = value;
}

//////////////////////////////////////////////////////////////////////////
// IHumanReadable
String Matrix3::ToString() const
{
	return (
		String::Format("[%.3f %.3f %.3f]\n", M11, M12, M13) +
		String::Format("|%.3f %.3f %.3f|\n", M21, M22, M23) +
		String::Format("[%.3f %.3f %.3f]"  , M31, M32, M33)
		);
}

//////////////////////////////////////////////////////////////////////////
// constants
const Matrix3 Matrix3::Zero     = Matrix3(0.0f, 0.0f, 0.0f,
										  0.0f, 0.0f, 0.0f,
										  0.0f, 0.0f, 0.0f);

const Matrix3 Matrix3::One      = Matrix3(1.0f, 1.0f, 1.0f,
										  1.0f, 1.0f, 1.0f,
									      1.0f, 1.0f, 1.0f);

const Matrix3 Matrix3::Identity = Matrix3(1.0f, 0.0f, 0.0f,
									      0.0f, 1.0f, 0.0f,
									      0.0f, 0.0f, 1.0f);

} } }
